package com.powernode.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
* @Author LF
* projectName:today-shop
* description:
* time:2022-09-17 14:54
*/

/**
    * 系统用户
    */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class LoginSysUser implements Serializable, UserDetails {
    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    /**
     * 用户名
     */
    @TableField(value = "username")
    private String username;

    /**
     * 密码
     */
    @TableField(value = "`password`")
    private String password;

    /**
     * 邮箱
     */
    //@TableField(value = "email")
    //private String email;

    /**
     * 手机号
     */
    //@TableField(value = "mobile")
    //private String mobile;

    /**
     * 状态  0：禁用   1：正常
     */
    @TableField(value = "`status`")
    private Byte status;

    /**
     * 创建者ID
     */
    //@TableField(value = "create_user_id")
    //private Long createUserId;

    /**
     * 创建时间
     */
    //@TableField(value = "create_time")
    //private Date createTime;

    /**
     * 用户所在的商城Id
     */
    @TableField(value = "shop_id")
    private Long shopId;

    /**
     * 用户所拥有的权限
     */
    @TableField(exist = false)
    private Set<String> auths;

    @TableField(exist = false)
    private String ip;


    public String getUsername(){
        return this.username;
    }
    public String getPassword(){
        return  this.password;
    }

    /**
     * 在auth-server中不需要处理授权解析
     * 分布式系统中 授权跟解析是分开的
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }

    public void setAuths(Set<String> auths) {
        HashSet<String> allAuths = new HashSet<>();
        // 处理权限的分割问题
        auths.forEach(auth -> {
            if (auth.contains(",")) {
                String[] realAuths = auth.split(",");
                for (String realAuth : realAuths) {
                    allAuths.add(realAuth);
                }
            } else {
                allAuths.add(auth);
            }
        });
        this.auths = allAuths;
    }
    //private static final long serialVersionUID = 1L;
    //
    //public static final String COL_USER_ID = "user_id";
    //
    //public static final String COL_USERNAME = "username";
    //
    //public static final String COL_PASSWORD = "password";
    //
    //public static final String COL_EMAIL = "email";
    //
    //public static final String COL_MOBILE = "mobile";
    //
    //public static final String COL_STATUS = "status";
    //
    //public static final String COL_CREATE_USER_ID = "create_user_id";
    //
    //public static final String COL_CREATE_TIME = "create_time";
    //
    //public static final String COL_SHOP_ID = "shop_id";
}